/*=========================================================================

Program:   Medical Imaging & Interaction Toolkit
Language:  C++
Date:      $Date: 2010-03-15 18:12:36 +0800 (周一, 15 三月 2010) $
Version:   $Revision: 21745 $

Copyright (c) German Cancer Research Center, Division of Medical and
Biological Informatics. All rights reserved.
See MITKCopyright.txt or http://www.mitk.org/copyright.html for details.

This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.  See the above copyright notices for more information.

=========================================================================*/


#include "WxContourMapperOverlay.h"

#include "mitkBaseRenderer.h"
#include "mitkPlaneGeometry.h"
#include "mitkColorProperty.h"
#include "mitkProperties.h"
#include "mitkContour.h"
#include <vtkLinearTransform.h>

#include "mitkGL.h"

mitk::WxContourMapperOverlay::WxContourMapperOverlay()
{
}

mitk::WxContourMapperOverlay::~WxContourMapperOverlay()
{
}


void mitk::WxContourMapperOverlay::Paint(mitk::BaseRenderer * renderer)
  {
  if(IsVisible(renderer)==false) return;

  ////  @FIXME: Logik fuer update
  bool updateNeccesary=true;

  if (updateNeccesary) 
    {
    mitk::Contour::Pointer input =  const_cast<mitk::Contour*>(this->GetInput());


    //apply color and opacity read from the PropertyList
    ApplyProperties(renderer);

    //    Contour::OutputType point;
    Contour::BoundingBoxType::PointType point;

    float lineWidth = 3.0;

    if (dynamic_cast<mitk::FloatProperty *>(this->GetDataNode()->GetProperty("Width")) != NULL)
      lineWidth = dynamic_cast<mitk::FloatProperty*>(this->GetDataNode()->GetProperty("Width"))->GetValue();
    glLineWidth(lineWidth);

    if (input->GetClosed())
      {
      glBegin (GL_LINE_LOOP);
      }
    else
      {
      glBegin (GL_LINE_STRIP);
      }

    //Contour::InputType end = input->GetContourPath()->EndOfInput();
    //if (end > 50000) end = 0;

    mitk::Contour::PointsContainerPointer points = input->GetPoints();
    mitk::Contour::PointsContainerIterator pointsIt = points->Begin();

    int i=0;
    while ( pointsIt != points->End() )
      {
      //while ( idx != end )
      //{
      //      point = input->GetContourPath()->Evaluate(idx);
      point = pointsIt.Value();
      glVertex2f(point[0], point[1]);
      pointsIt++;
      i++;
      //      idx += 1;
      }
    glEnd ();


    glLineWidth(1.0);

    }
  }

const mitk::Contour* mitk::WxContourMapperOverlay::GetInput(void)
{
  return static_cast<const mitk::Contour * > ( GetData() );
}
bool mitk::WxContourMapperOverlay::ipMITKSegmentationIsInsideContour( float *contour, int sizeContour, float x, float y )
{

  int i, j;
  bool res = false;
  for ( i=0, j=sizeContour-1;
        i<sizeContour;
        j=i++)
  {
    if (
            (     ((contour[2*i+1]<=y) && (y<contour[2*j+1]))
               || ((contour[2*j+1]<=y) && (y<contour[2*i+1]))  )
         &&
            (  x < (contour[2*j] - contour[2*i]) * (y - contour[2*i+1]) / (contour[2*j+1] - contour[2*i+1]) + contour[2*i])
       )

      res = !res;
  }
  return res;
}
